//
//  MainViewController.m
//  Weibo-OC
//
//  Created by 焦林生 on 15/11/20.
//  Copyright © 2015年 cn.baidu. All rights reserved.
//
#import "NetworkTools.h"

@interface MainViewController ()
@property (nonatomic, weak) UIButton *btn;
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupAllChildVc];
    
    [self setupComposeButton];
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    //创建tababr中所有控制器对应的按钮
    [super viewWillAppear:animated];
    //将撰写按钮弄到最前面
    [self.tabBar bringSubviewToFront:self.btn];
}


- (void)setupComposeButton {
    //自定义撰写按钮
    UIButton *composeButton = [UIButton norImage:@"tabbar_compose_icon_add" highImage:@"tabbar_compose_icon_add_highlighted" backImageNameNor:@"tabbar_compose_button" backImageNameHigh:@"tabbar_compose_button_highlighted"];
    //获取撰写按钮
    self.btn = composeButton;
    
    //1 添加按钮
    [self.tabBar addSubview:self.btn];
    //2 调整按钮
    CGFloat count = self.childViewControllers.count;
    
    CGFloat w = self.tabBar.bounds.size.width / count - 1;
    
    self.btn.frame = CGRectInset(self.tabBar.bounds, 2 * w, 0);
    
    
    //3 添加监听方法
    [self.btn addTarget:self action:@selector(clickComposeButton) forControlEvents: UIControlEventTouchUpInside];
    
}

- (void)clickComposeButton {

    NSLog(@"点击撰写按钮");
}

//添加子控件
- (void)setupAllChildVc {
    
    
    HomeViewController *Hvc = [[HomeViewController alloc]init];
    [self addChildVc:Hvc title:@"首页" imageName:@"tabbar_home"];
    
    MessageViewController *Mvc = [[MessageViewController alloc]init];
    [self addChildVc:Mvc title:@"消息" imageName:@"tabbar_message_center"];
    
    ComposeViewController *Cvc = [[ComposeViewController alloc]init];
    [self addChildViewController:Cvc];
    
    DiscoverViewController *Dvc = [[DiscoverViewController alloc]init];
    [self addChildVc:Dvc title:@"发现" imageName:@"tabbar_discover"];
    
    ProfileViewController *Pvc = [[ProfileViewController alloc]init];
    [self addChildVc:Pvc title:@"我" imageName:@"tabbar_profile"];
    
    
}

//封装添加控制器(多态 : 父类的指针指向子类的对象)
- (void)addChildVc:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)imageName {

    //1设置标题
    vc.title = title;
    
    //2 设置图片
    vc.tabBarItem.image = [UIImage imageNamed:imageName];
    
    //3给每个子控制器添加导航控制器(子控制器作为导航控制器的跟控制器)
    NavigationController *nav = [[NavigationController alloc]initWithRootViewController:vc];
    
    //导航控制器添加到tabbar控制器上
    [self addChildViewController:nav];
}


@end
